package com.ruoyi.materialCuttingPlan.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.materialCuttingPlan.domain.McpVariable;
import com.ruoyi.materialCuttingPlan.mapper.McpWindowTypesMapper;
import com.ruoyi.materialCuttingPlan.domain.McpWindowTypes;
import com.ruoyi.materialCuttingPlan.service.IMcpWindowTypesService;

/**
 * 门窗类型定义Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-05
 */
@Service
public class McpWindowTypesServiceImpl implements IMcpWindowTypesService 
{
    @Autowired
    private McpWindowTypesMapper mcpWindowTypesMapper;

    /**
     * 查询门窗类型定义
     * 
     * @param id 门窗类型定义主键
     * @return 门窗类型定义
     */
    @Override
    public McpWindowTypes selectMcpWindowTypesById(Long id)
    {
        return mcpWindowTypesMapper.selectMcpWindowTypesById(id);
    }

    /**
     * 查询门窗类型定义列表
     * 
     * @param mcpWindowTypes 门窗类型定义
     * @return 门窗类型定义
     */
    @Override
    @DataScope(userAlias = "u")
    public List<McpWindowTypes> selectMcpWindowTypesList(McpWindowTypes mcpWindowTypes)
    {
        return mcpWindowTypesMapper.selectMcpWindowTypesList(mcpWindowTypes);
    }

    /**
     * 新增门窗类型定义
     * 
     * @param mcpWindowTypes 门窗类型定义
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMcpWindowTypes(McpWindowTypes mcpWindowTypes)
    {
        mcpWindowTypes.setCreateTime(DateUtils.getNowDate());
        mcpWindowTypes.setCreateBy(SecurityUtils.getUsername());
        mcpWindowTypes.setCreateById(SecurityUtils.getUserId());
        int rows = mcpWindowTypesMapper.insertMcpWindowTypes(mcpWindowTypes);
        insertMcpVariable(mcpWindowTypes);
        return rows;
    }

    /**
     * 修改门窗类型定义
     * 
     * @param mcpWindowTypes 门窗类型定义
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMcpWindowTypes(McpWindowTypes mcpWindowTypes)
    {
        mcpWindowTypes.setUpdateTime(DateUtils.getNowDate());
        mcpWindowTypes.setUpdateBy(SecurityUtils.getUsername());
        mcpWindowTypes.setUpdateById(SecurityUtils.getUserId());
        mcpWindowTypesMapper.deleteMcpVariableByWindowTypesId(mcpWindowTypes.getId());
        insertMcpVariable(mcpWindowTypes);
        return mcpWindowTypesMapper.updateMcpWindowTypes(mcpWindowTypes);
    }

    /**
     * 批量删除门窗类型定义
     * 
     * @param ids 需要删除的门窗类型定义主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMcpWindowTypesByIds(Long[] ids)
    {
        mcpWindowTypesMapper.deleteMcpVariableByWindowTypesIds(ids);
        return mcpWindowTypesMapper.deleteMcpWindowTypesByIds(ids);
    }

    /**
     * 删除门窗类型定义信息
     * 
     * @param id 门窗类型定义主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMcpWindowTypesById(Long id)
    {
        mcpWindowTypesMapper.deleteMcpVariableByWindowTypesId(id);
        return mcpWindowTypesMapper.deleteMcpWindowTypesById(id);
    }

    /**
     * 新增门窗用料公式变量信息
     * 
     * @param mcpWindowTypes 门窗类型定义对象
     */
    public void insertMcpVariable(McpWindowTypes mcpWindowTypes)
    {
        List<McpVariable> mcpVariableList = mcpWindowTypes.getMcpVariableList();
        Long id = mcpWindowTypes.getId();
        if (StringUtils.isNotNull(mcpVariableList))
        {
            List<McpVariable> list = new ArrayList<McpVariable>();
            for (McpVariable mcpVariable : mcpVariableList)
            {
                mcpVariable.setWindowTypesId(id);
                mcpVariable.setCreateTime(DateUtils.getNowDate());
                mcpVariable.setCreateBy(SecurityUtils.getUsername());
                mcpVariable.setCreateById(SecurityUtils.getUserId());
                list.add(mcpVariable);
            }
            if (list.size() > 0)
            {
                mcpWindowTypesMapper.batchMcpVariable(list);
            }
        }
    }
}
